package com.khh.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.khh.base.assertion.AssertValue;


public class DateUtils {

	private static final Logger log = Logger.getLogger(DateUtils.class);

	/**
	 * 将字符串转换成日期
	 * 
	 * @param str
	 * @return
	 */
	public static Date convertToDate(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		if (AssertValue.isNotNullAndNotEmpty(time)) {
			try {
				Date val = sdf.parse(time);
				return val;
			} catch (ParseException e) {

				System.out.println("日期转换出现异常！" + e.getMessage());
				return null;
			}

		} else {
			return null;
		}
	}
	
	/**
	 * 将字符串转换成日期
	 * 
	 * @param str
	 * @return
	 */
	public static String convertToDate(Date time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		if (AssertValue.isNotNull(time)) {
			String val = sdf.format(time);
			return val;
		} else {
			return null;
		}
	}
	
	/**
	 * 将字符串转换成日期
	 * 
	 * @param str
	 * @return
	 */
	public static String convertToDate(Date time,String formatstr) {
		SimpleDateFormat sdf = new SimpleDateFormat(formatstr);

		if (AssertValue.isNotNull(time)) {
			String val = sdf.format(time);
			return val;
		} else {
			return null;
		}
	}
}
